home *** CD-ROM | disk | FTP | other *** search
- Path: locutus.rchland.ibm.com!usenet
- From: pstaite@vnet.ibm.com
- Newsgroups: comp.lang.c++
- Subject: Re: Question about destructor...
- Date: 3 Jan 1996 18:48:03 GMT
- Organization: IBM OS/2 Device Driver Development Rochester, MN
- Message-ID: <4cej13$usb@locutus.rchland.ibm.com>
- References: <4ce9mk$atg@eng_ser1.erg.cuhk.hk>
- Reply-To: pstaite@vnet.ibm.com
- NNTP-Posting-Host: warpone.rchland.ibm.com
- X-Newsreader: IBM NewsReader/2 v1.2
-
- In <4ce9mk$atg@eng_ser1.erg.cuhk.hk>, ywleung@cs.cuhk.hk (Marty McFly) writes:
- >Dear All,
- >
- > I now have 2 classes A and B. B is a derived class of A. However,
- >the internal implementation of A is not known, i.e., the private members of
- >class A are not provided, only the public members are given. But class B is
- >implemented by myself. So what should be written in the destructor of B so
- >that all the private members inherited from A are also "deleted"?
-
- In a word, nothing. When you destroy a B object the destructor for the
- A part of it will be called automatically. Try this little example:
-
- #include<iostream.h>
-
- class A {
- public:
- A() { cout << "A::A()" << endl; }
- ~A() { cout << "A::~A()" << endl; }
- };
-
- class B : public A {
- public:
- B() { cout << "B::B()" << endl; }
- ~B() { cout << "B::~B()" << endl; }
- };
-
- int main() {
- cout << "Making a B object:" << endl;
- B *pb( new B );
- cout << "Destroying a B object" << endl;
- delete pb;
- return 0; }
-
- When run this should produce:
-
- Making a B object:
- A::A()
- B::B()
- Destroying a B object
- B::~B()
- A::~A()
-
- Note that the A part of the B is constructed first, a destructed last.
- Think of it as the "foundation" for the B -- that is, the B class builds
- on top of it.
-
-
- Phil Staite, team OS/2
- internet: pstaite@vnet.ibm.com internal: pstaite@rchland
-
-